import Vue from 'vue'
import Router from 'vue-router'
import Home from "../views/Home";
import User from '../views/pages/user'
import Role from '../views/pages/role'
import Permission from '../views/pages/permission'
import Login from "../views/Login";
import store from '../store'
Vue.use(Router)

const router = new Router({
  routes: [,

    {
      path:'/',
      redirect:'/home'
    },

    {
      path:'/login',
      component:Login
    },

    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect:'/system/user',
      children:[
        {
          path:'/system/user',
          name:'User',
          component: User
        },
        {
          path:'/system/role',
          name:'Role',
          component: Role
        },
        {
          path:'/system/permission',
          name:'Permission',
          component:Permission
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if(to.path!='/login'){
    if(!store.getters.token){
      next('/login')
    }else{
      next();
    }
  }
  else {
    if(store.getters.token){
      next('/home');
    }else{
      next();
    }
  }

})

export default router;
